﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace PersonalFinances.Models.BusinessLogic
{
    public class FinancesInitializer : DropCreateDatabaseAlways<FinancesDBContext>
    {
        protected override void Seed(FinancesDBContext context)
        {
            var inCategories = new List<Category>() 
            {
                new Category() 
                {
                    InOrOut = true,
                    Name = "Salary",
                    Periodicity = 30,
                    Value = 962,
                    Notes = new List<Note>()
                    {
                        new Note() 
                        {
                            Date = new DateTime(2011,10,10),
                            Value = 962
                        }
                    }
                }
            }; 
            var outCategories = new List<Category>() 
            {
                new Category() 
                {
                    InOrOut = false,
                    Name = "Fuel",
                    Periodicity = 10,
                    Value = 40,
                    Notes = new List<Note>()
                    {
                        new Note() 
                        {
                            Date = new DateTime(2011,10,10),
                            Value = 40
                        }
                    }
                },
                new Category() 
                {
                    InOrOut = false,
                    Name = "Tolls",
                    Periodicity = 1,
                    Value = 2.54,
                    Notes = new List<Note>()
                    {
                        new Note() 
                        {
                            Date = new DateTime(2011,10,10),
                            Value = 2.54
                        }
                    }
                },
                new Category() 
                {
                    InOrOut = false,
                    Name = "Food",
                    Periodicity = 7,
                    Value = 10,
                    Notes = new List<Note>()
                    {
                        new Note() 
                        {
                            Date = new DateTime(2011,10,14),
                            Value = 10
                        }
                    }
                }
            };

            inCategories.ForEach(iC => iC.Notes.ForEach(note => note.Category = iC));
            inCategories.ForEach(iC => context.Categories.Add(iC));
            outCategories.ForEach(oC => oC.Notes.ForEach(note => note.Category = oC));
            outCategories.ForEach(oC => context.Categories.Add(oC));
            GenerateNotes(inCategories, context);
            GenerateNotes(outCategories,context);
        }

        private void GenerateNotes(List<Category> categories,FinancesDBContext context)
        {
            foreach (var cat in categories)
            {
                DateTime fDate = cat.Notes.First().Date;
                int fValue = fDate.Day;
                context.Notes.Add(cat.Notes.First());
                for (int i = fValue; i <= 365; i += cat.Periodicity)
                {
                    var temp = new Note() { Category = cat, Date = fDate.AddDays(i), Value = cat.Value };
                    cat.Notes.Add(temp);
                    context.Notes.Add(temp);
                }
            }
        }
    }
}